﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.DirectoryServices;
using System.Net.Sockets;
using System.Configuration;
using System.Net.Mail;
using Newtonsoft.Json;

namespace TrialManagementTool.Helper
{
    public static class Common
    {

        private static string findLDAPServer()
        {
            string[] LDAPIPs = {"usmdlddowd024.dow.com","163.198.215.45", //usmdlddowd024.dow.com
                                "usfrpddowd004.dow.com","164.155.109.37", //usfrpddowd004.dow.com
                                "sgsdcddowd006.dow.com","144.67.150.252", //sgsdcddowd006.dow.com
                                "cnshgddowd001.dow.com","144.68.95.201", //cnshgddowd001.dow.com
                                "nltnzddowd003.dow.com","144.67.63.169" //nltnzddowd003.dow.com
                        };
            string selectedLDAPIP = "";
            bool canConnectLDAP = false;

            System.Net.Sockets.TcpClient client = new TcpClient();

            foreach (string LDAPIP in LDAPIPs)
            {
                if (canConnectLDAP == false)
                {
                    try
                    {
                        client.Connect(LDAPIP, 389);
                        Console.WriteLine("Connection open, host active");
                        //LogHelper.Info(string.Format("findLDAPServer,the server is {0}", LDAPIP));
                        canConnectLDAP = true;
                        selectedLDAPIP = LDAPIP;
                    }
                    catch (SocketException ex)
                    {
                        LogHelper.Error("Connection could not be established due to: \n" + ex.Message);
                        Console.WriteLine("Connection could not be established due to: \n" + ex.Message);
                        canConnectLDAP = false;
                    }
                }
                else
                {
                    break;
                }
            }
            client.Close();

            return selectedLDAPIP;
        }

        public static List<String> authenticateUser(string domain, string username, string password)
        {
            string statusCode = ""; //0 means successful, 1 means user not found, 2 means system error
            string status = "";
            List<String> returnValue = new List<String>();

            //string domain = Request.QueryString["domain"];
            //string username = Request.QueryString["username"];
            //string password = Request.QueryString["password"];
            string filterAttribute = "";


            string selectedLDAPIP = findLDAPServer();
            if (selectedLDAPIP == "")
            {
                // LDAP cannot be reached, return error
                statusCode = "2";
                status = "无法连接验证服务器，请稍候再尝试。";
                returnValue.Add(statusCode);
                returnValue.Add(status);
                return returnValue;
            }

            string selectedLDAP = "LDAP://" + selectedLDAPIP + "/DC=DOW,DC=COM";

            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(selectedLDAP, domainAndUsername, password);

            // lookup for this person
            try
            {
                //Bind to the native AdsObject to force authentication.
                object obj = entry.NativeObject;
                //LogHelper.Info("found the user info:" + JsonConvert.);
                statusCode = "0"; // ID found
                status = "已验证";
                returnValue.Add(statusCode);
                returnValue.Add(status);
                return returnValue;

            }
            catch (Exception ex)
            {
                statusCode = "2"; // Error authenticating user
                status = "无法验证用户: " + ex.Message;
                //LogHelper.Error("Authentication user failed in LDAP");
                returnValue.Add(statusCode);
                returnValue.Add(status);
                return returnValue;
            }
        }

        public static string ConvertNumToChinese(int i)
        {
            try
            {
                string[] array1 = new string[] { "", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
                string[] array2 = new string[] { "", "十", "二十", "三十", "四十", "五十", "六十", "七十", "八十", "九十", "一O" };
                int ten = i / 10;
                int yushu = i % 10;
                return array2[ten] + array1[yushu];
            }
            catch
            {
                return "";
            }
        }
    }
}
